package com.fs.navigatortest;

import android.os.Bundle;
import android.view.MenuItem;

import com.fs.navigatortest.ui.dashboard.DashboardFragment;
import com.fs.navigatortest.ui.home.HomeFragment;
import com.fs.navigatortest.ui.notifications.NotificationsFragment;
import com.fs.navigatortest.utils.FixFragmentNavigator;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.NavGraph;
import androidx.navigation.NavGraphNavigator;
import androidx.navigation.Navigation;
import androidx.navigation.NavigatorProvider;
import androidx.navigation.fragment.FragmentNavigator;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.fs.navigatortest.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    Fragment myFragment  = null;
   // BottomNavigationView menuNavigation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        //  menuNavigation = findViewById(R.id.menu_navigation);

        myFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_main);
        //fragment的重复加载问题和NavController有关
        NavController navController = NavHostFragment.findNavController(myFragment);
         setFixNavigator(navController);
    }


    /**
     * FixFragmentNavigator的使用
     */
    private void setFixNavigator(NavController navController) {
        NavigatorProvider provider = navController.getNavigatorProvider();
        //设置自定义的navigator
        FixFragmentNavigator fixFragmentNavigator =
               new  FixFragmentNavigator(this, myFragment.getChildFragmentManager(), myFragment.getId());
        provider.addNavigator(fixFragmentNavigator);


        NavGraph navDestinations = initNavGraph(provider, fixFragmentNavigator);
        navController.setGraph(navDestinations);// = navDestinations!!
        binding.menuNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                navController.navigate(item.getItemId());
                return true;
            }
        });

    }


    private NavGraph initNavGraph(
            NavigatorProvider provider ,
            FixFragmentNavigator fragmentNavigator
    )  {
        NavGraph navGraph =new  NavGraph(new NavGraphNavigator(provider));

        //用自定义的导航器来创建目的地
        FragmentNavigator.Destination destination1 = fragmentNavigator.createDestination();
        destination1.setId(R.id.navigation_home); //= R.id.AFragment
        destination1.setClassName(HomeFragment.class.getCanonicalName())  ;//= AFragment::class.java.getCanonicalName()
        navGraph.addDestination(destination1);


        FragmentNavigator.Destination destination2 = fragmentNavigator.createDestination();
        destination2.setId(R.id.navigation_dashboard); //= R.id.AFragment
        destination2.setClassName(DashboardFragment.class.getCanonicalName());
       // destination1.setClassName(HomeFragment)  ;//= AFragment::class.java.getCanonicalName()
        navGraph.addDestination(destination2);

        FragmentNavigator.Destination destination3 = fragmentNavigator.createDestination();
        destination3.setId(R.id.navigation_notifications); //= R.id.AFragment
        destination3.setClassName(NotificationsFragment.class.getCanonicalName());
        // destination1.setClassName(HomeFragment)  ;//= AFragment::class.java.getCanonicalName()
        navGraph.addDestination(destination3);
        navGraph.setStartDestination(R.id.navigation_home);


        return navGraph;
    }





}